/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.cast.s3p.tlds;

import com.cast.s3p.controladores.Precioso;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.TreeMap;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTag;
import javax.servlet.jsp.tagext.Tag;

/**
 *
 * @author carlrami
 */
public class listadoItemsEtiqueta implements BodyTag{
    PageContext contextoDePagina;
    private String tipo = null;
    private String metodo = null;
    private String proceso = null;
    private String accion = null;
    Precioso gollumGL;
    TreeMap resultado = new TreeMap();
    Method metodoLlamado;

    public void setTipo(String x) { this.tipo = x; }
    public void setMetodo(String x) { this.metodo = x; }
    public void setProceso(String x) { this.proceso = x; }
    public void setAccion(String x) { this.accion = x; }
    
    public void setBodyContent(BodyContent b) {}
    public void doInitBody() throws JspException {}

    public int doAfterBody() throws JspException { return EVAL_PAGE; }

    public void setPageContext(PageContext pc) { this.contextoDePagina = pc; }
    public void setParent(Tag t) {}
    public Tag getParent() { throw new UnsupportedOperationException("Not supported yet."); }

    public int doStartTag() throws JspException {
        ServletContext contextoDeServlet = this.contextoDePagina.getServletContext();
        HttpSession sesion = this.contextoDePagina.getSession();
        this.gollumGL = (Precioso) sesion.getAttribute("Precioso");
        Class parametros[] = {proceso.getClass(), accion.getClass()};
        Object argumentos[] = {proceso, accion};

        try {
            
            metodoLlamado = this.gollumGL.getClass().getDeclaredMethod(metodo, parametros);
            resultado = (TreeMap) metodoLlamado.invoke(this.gollumGL, argumentos);
            
        } catch (IllegalAccessException ex) {

        } catch (IllegalArgumentException ex) {

        } catch (InvocationTargetException ex) {

        } catch (NoSuchMethodException ex) {

        } catch (SecurityException ex) {

        }
        
        if (resultado != null && resultado.size() > 0) {
            sesion.setAttribute(tipo.toString(), this.resultado);
            return EVAL_PAGE;
        } else {
            return SKIP_PAGE;
        }
    }

    public int doEndTag() throws JspException { return EVAL_PAGE; }
    
    public void release() {
        this.contextoDePagina = null;
        this.gollumGL = null;
        this.resultado = null;
        this.metodoLlamado = null;
    }
}